package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CutomerTransfer;
import cn.wolfcode.crm.mapper.CutomerTransferMapper;
import cn.wolfcode.crm.query.CutomerTransferQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.service.ICutomerTransferService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CutomerTransferServiceImpl implements ICutomerTransferService {

    @Autowired
    private CutomerTransferMapper mapper;
    @Autowired
    private IClientService clientService;

    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int insert(CutomerTransfer record) {
        return mapper.insert(record);
    }

    public CutomerTransfer selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<CutomerTransfer> selectAll() {
        return selectAll();
    }

    public int updateByPrimaryKey(CutomerTransfer record) {
        return mapper.updateByPrimaryKey(record);
    }

    public PageResult list(CutomerTransferQueryObject qo) {
        int count = mapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, mapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    public void transfer(CutomerTransfer obj) {
        clientService.updateInChargeUserByPrimaryKey(obj.getNewSeller().getId(),obj.getCustomer().getId());
        obj.setTransTime(new Date());
        obj.setTransUser(UserContext.getCurrentUser());
        System.out.println(obj);
        mapper.insert(obj);
    }
}
